<?php

namespace common\models\freight;

use Yii;
use yii\helpers\ArrayHelper;
use common\models\base\BaseOptions;
use common\models\base\BaseDistrict;

/**
 * This is the model class for table "{{%freight_template}}".
 *
 * @property int $id
 * @property string $name 模版名称
 * @property string $description
 * @property string $province 省
 * @property string $city 市
 * @property string $district 区
 * @property int $free 是否包邮
 * @property int $pricing_mode 计价方式：0按件，1按重量，2按体积
 * @property string $deliver_period 发货周期
 * @property string $admin_id 创建者ID
 * @property int $status 状态
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at 删除时间
 */
class FreightTemplate extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const FREE_NO = 0;
    const FREE_YES = 1;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%freight_template}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'province', 'city', 'district'], 'required'],
            [['free', 'pricing_mode', 'deliver_period', 'admin_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'province', 'city', 'district'], 'string', 'max' => 50],
            [['description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '模版名称',
            'description' => '备注',
            'province' => '省',
            'city' => '市',
            'district' => '区',
            'free' => '包邮',
            'pricing_mode' => '计价方式',
            'deliver_period' => '发货周期',
            'admin_id' => '创建者ID',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getModes() {
        return $this->hasMany(FreightMode::className(), ['template_id' => 'id']);
    }

    public function getExtendMode() {
        return $this->getModes()->andWhere(['!=', 'parent_id', 0]);
    }

    public function getDefaultMode() {
        return $this->hasOne(FreightMode::className(), ['template_id' => 'id'])->andWhere(['parent_id' => 0]);
    }

    public static function getOptionList($attribute, $fill = null) {
        $data = ArrayHelper::getValue(BaseOptions::getOptionList(), $attribute, []);
        if ($fill !== null && !empty($data)) {
            foreach ($data as $key => $vls) {
                $data[$key] = $vls . $fill;
            }
        }
        return $data;
    }

    public function getAddressText() {
        return BaseDistrict::getUnifinedName([$this->city]);
    }

    /**
     * @return array
     */
    public static function activeList() {
        $data = static::findAll([
            'status' => static::STATUS_ACTIVE,
            'deleted_at' => 0,
        ]);
        return ArrayHelper::map($data, 'id', 'name');
    }

}
